package com.blockmeta.bbs.businesslibrary;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.provider.IMApplicationContextProvider;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.util.ProcessHelper;

import java.util.List;

/**
 * 判断模块是否存在进行初始化，防止内存被回收后无法重启
 */
public class BaseBussinessApp extends BaseApp {
    private static BaseBussinessApp mContext;
    public static final int STATUS_FORCE_KILLED = -1;//应用在后台被强杀了
    public static final int STATUS_NORMAL = 2; //APP正常态
    private int appStatus = STATUS_FORCE_KILLED; //默认为被后台回收了

    public static BaseBussinessApp getApp() {
        return mContext;
    }

    public int getAppStatus() {
        return appStatus;
    }

    //在所有app的正常
    public void setAppStatus(int appStatus) {
        this.appStatus = appStatus;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        //多进程下会oncreate多次
        if(ProcessHelper.isMainProcess(this)){
            initIMIfExist();
        }else {
        }
    }

    /**
     * 初始化im模块，需要在arouter初始化后运行
     */
    private void initIMIfExist() {
        IMApplicationContextProvider provider = RouterHelper.getProvider(RouterPath.IM_APPLICATIONCONTEXT_PROVIDER);
        if(provider != null){
            provider.onCreate(mContext);
        }
    }

    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }



}
